﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Project.Tool
{
    /// <summary>
    /// 异步操作
    /// </summary>
    public class AsyncHelper
    {
        /// <summary>
        /// 开启一个线程
        /// </summary>
        /// <param name="fun"></param>
        public static Thread NewThread(Action fun)
        {
            //将工作项加入到线程池队列中，这里可以传递一个线程参数
            //ThreadPool.QueueUserWorkItem(fun, "Hello");
            return new Thread(new ThreadStart(fun));
        }

        /// <summary>
        /// 开启一个带参数线程
        /// </summary>
        /// <param name="fun"></param>
        /// <returns></returns>
        public static Thread NewThread(Action<object> fun)
        {
            return new Thread(new ParameterizedThreadStart(fun));
        }

        /// <summary>
        /// 异步执行
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public async static Task<T> Await<T>(Func<T> fun)
        {
            return await Task.Run<T>(fun);
        }
        
    }
}
